package com.autonavi.abs.common.util;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

/**
 * @author wwy
 * @version V1.0
 * <p>Description: base64 util</p>
 * @date 2020-07-23 12:09
 */
public class Base64Utils {

    /**
     * 字符串转base64
     *
     * @param str
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String encode(String str) throws UnsupportedEncodingException {
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(str.getBytes("utf-8"));
    }

    /**
     * base64转字符串
     *
     * @param str
     * @return
     * @throws NoSuchAlgorithmException
     * @throws IOException
     */
    public static String decode(String str) throws IOException {
        BASE64Decoder decoder = new BASE64Decoder();
        return new String(decoder.decodeBuffer(str), "utf-8");
    }
}
